package com.xiaoyu.lanling.feature.vip.fragment

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import android.os.Bundle
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.view.list.GridSpacingItemDecoration
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.vip.VipMineEvent
import com.xiaoyu.lanling.feature.vip.data.VipData
import com.xiaoyu.lanling.feature.vip.model.PrivilegeItem
import com.xiaoyu.lanling.feature.vip.viewholder.PrivilegeViewHolder
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import kotlinx.android.synthetic.main.vip_mine_fragment.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VipMineFragment : BaseFragment() {

    private val vipData = VipData()
    private val requestTag = Any()

    private val adapter = CubeRecyclerViewSimpleAdapter<PrivilegeItem>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.vip_mine_fragment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)

        initAdapter()
        initBind()
        initData()
        initEvent()
    }

    private fun initAdapter() {
        adapter.setViewHolderClass(0, null, PrivilegeViewHolder::class.java, 9)
        privilege_recyclerview.adapter = adapter
        privilege_recyclerview.addItemDecoration(GridSpacingItemDecoration(3, 48, 16, false))
    }

    private fun initBind() {
        renew_button.setOnClickDebounceListener {
            activity?.let {
                Router.instance.gotoVipProductListActivity(it, Constant.From.VIP_CENTER)
            }
        }
    }

    private fun initData() {
        val user = UserData.getInstance().user
        ImageViewUtil.loadImage(avatar, user, 48)
        name.setUser(user)
        vipData.getMyVip(requestTag)
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: VipMineEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                showData(event)
            }
        })
    }

    private fun showData(event: VipMineEvent) {
        val spannableDateString = SpannableString(getString(R.string.vip_mine_end_date_desc, event.vipProduct.vipInfo.name, event.endDate))
        spannableDateString.setSpan(ForegroundColorSpan(AppContext.getColor(R.color.vip_mine_date_desc)), spannableDateString.indexOf(event.endDate), spannableDateString.length, SpannableString.SPAN_INCLUSIVE_INCLUSIVE)
        end_date_desc.text = spannableDateString

        val privilegeSize = event.vipProduct.privileges.size
        val privilegeDesc = "$privilegeSize/$privilegeSize"
        privilege_overview.text = getString(R.string.vip_product_privilege_overview_title, event.vipProduct.vipInfo.name, privilegeDesc)
        ImageViewUtil.loadImage(vip_icon, ImageLoadParam.newBuilder().setUrl(event.vipProduct.vipInfo.icon).setSizeInDP(132).build())

        adapter.list = event.vipProduct.privileges
        adapter.notifyDataSetChanged()
    }
}